public class Solution29 {
    public int divide(int dividend, int divisor) {
        boolean symbol = true;
        if (dividend > 0) {
            dividend = -dividend;
            symbol = false;
        }
        if (divisor > 0) {
            divisor = -divisor;
            symbol = !symbol;
        }
        int res = 0;
        while (dividend <= divisor) {
            int n = 1, t;
            while (true) {
                t = dividend >> n;
                if (t >= divisor) {
                    res -= 1 << (n - 1);
                    dividend -= divisor << (n - 1);
                    break;
                }
                n++;
            }
        }

        return symbol ? (res == Integer.MIN_VALUE ? Integer.MAX_VALUE : -res) : res;
    }
}
